package com.dailyblue.java.standard.thread.demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Dealer {
    // 仓库
    private Stack<Goods> list = new Stack<>();
    // 最大容量
    private final static int MAX_COUNT = 10;

    // 入库
    public synchronized void restock(Goods g) {
        if (list.size() == MAX_COUNT) {
            System.out.println("库存已满，不能在进货了！");
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        list.push(g);
        this.notify();
        System.out.println("供应商进货，商品：" + g);
    }

    // 售出
    public synchronized Goods shipping() {
        if (list.size() == 0) {
            System.out.println("没有商品了，缺货！请稍等！");
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Goods g = list.pop();
        this.notify();
        return g;
    }
}
